package com.lili.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lili.base.context.WebContext;
import com.lili.config.cache.RedisCache;
import com.lili.mapper.ClassesMapper;
import com.lili.pojo.*;
import com.lili.service.ClassesService;
import com.lili.utils.RabbitMQUtil;
import com.lili.vo.request.officer.ClassesReqVO;
import com.lili.vo.response.officer.ClassesRespVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.concurrent.TimeUnit;

@Service
public class ClassesServiceImpl extends ServiceImpl<ClassesMapper, Classes>
        implements ClassesService {

    private final RabbitMQUtil rabbitMQUtil;

    private final WebContext webContext;

    private final RedisCache redisCache;

    @Autowired
    public ClassesServiceImpl(RabbitMQUtil rabbitMQUtil, WebContext webContext, RedisCache redisCache) {
        this.rabbitMQUtil = rabbitMQUtil;
        this.webContext = webContext;
        this.redisCache = redisCache;
    }

    @Override
    public List<Classes> selectByClassName(String classesName) {
        LambdaQueryWrapper<Classes> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(!classesName.equals("_"), Classes::getClassesName, classesName)
                .eq(Classes::getDeleted, false);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public IPage<ClassesRespVO> selectPage(ClassesReqVO vo) {

        IPage<ClassesRespVO> page = new Page<>(vo.getPageIndex(), vo.getPageSize());

        IPage<ClassesRespVO> pages = baseMapper.selectPageList(page, vo);
        return pages;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(Classes vo, Integer id) {
        // 新增编班
        if (Objects.isNull(vo.getId())) {
            vo.setCreateUser(id);
            vo.setDeleted(0);
            vo.setCreateTime(new Date());
            this.save(vo);
        } else {
            baseMapper.updateById(vo);
            redisCache.deleteObject("clazz:" + vo.getId());
        }
    }

    @Override
    public void delBatch(List<Integer> ids) {
        //清理缓存
        ids.stream().forEach(item -> redisCache.deleteObject("clazz:" + item));
        baseMapper.delBatch(ids);
    }

    /**
     * 得到编班ID
     *
     * @param id
     * @return
     */
    @Override
    public Classes getClassById(Integer id) {
        String key = "clazz:" + id;
        Classes clazz = null;
        Object obj = redisCache.getCacheObject(key);
        if (!Objects.isNull(obj)) {
            clazz = (Classes) obj;
            return clazz;
        }
        clazz = baseMapper.selectById(id);
        redisCache.setCacheObject(key, clazz, 10, TimeUnit.MINUTES);
        return clazz;
    }


/*    public void send(Log log){
        rabbitTemplate.convertAndSend(RabbitMQConstant.EXAM_EXCHANGE,RabbitMQConstant.LOG_KEY,log);
    }*/

    private User getUser() {
        return webContext.getCurrentUser();
    }

}




